home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Pentominoes 1.4.1 / source / Pentominoes ƒ / Pent code / pent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  8.8 KB  |  383 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        pent.c
  4.  
  5. Purpose:    This module handles game initialization/shutdown and
  6.             mouse and key events during a game.
  7.  
  8.  
  9. Pentominoes - a 2-D geometry board game
  10. Copyright (C) 1993 Mark Pilgrim
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "pent.h"
  30. #include "pent setup.h"
  31. #include "pent meat.h"
  32. #include "pent load-save.h"
  33. #include "pent generic open.h"
  34. #include "pent files.h"
  35. #include "msg environment.h"
  36. #include "msg sounds.h"
  37. #include "msg graphics.h"
  38. #include "msg menus.h"
  39. #include "msg dialogs.h"
  40.  
  41. CIconHandle        gColorIcons[12];
  42. Handle            gBWIcons[12];
  43. PixPatHandle    gPlainColors[15];
  44. PatHandle        gBWPatterns[15];
  45. int                gShapes[12][5][5];
  46.  
  47. char            SolutionLetters[14];
  48. int                gWhichBoard;
  49. int                gNumHilited;
  50. int                Board[8][20];
  51. int                gNumRows;
  52. int                gNumCols;
  53. Boolean            gShowGrid;
  54. Boolean            PieceUsed[12];
  55. int                gNumPlayed;
  56. int                PiecesPlayed[12];
  57.  
  58. long            oldTicks;
  59. int                gCurrentColor;
  60. Boolean            freshClick;
  61.  
  62. void InitGame(void)
  63. {
  64.     int            i,j,k;
  65.     AppFile        myFile;
  66.     
  67.     SolutionLetters[0]=' ';
  68.     SolutionLetters[1]='A';
  69.     SolutionLetters[2]='H';
  70.     SolutionLetters[3]='D';
  71.     SolutionLetters[4]='B';
  72.     SolutionLetters[5]='L';
  73.     SolutionLetters[6]='F';
  74.     SolutionLetters[7]='G';
  75.     SolutionLetters[8]='C';
  76.     SolutionLetters[9]='J';
  77.     SolutionLetters[10]='K';
  78.     SolutionLetters[11]='E';
  79.     SolutionLetters[12]='I';
  80.     SolutionLetters[13]='Z';
  81.     
  82.     gHelpHeight=200;
  83.     gHelpWidth=300;
  84.     
  85.     for (i=0; i<12; i++)
  86.     {
  87.         if (gHasColorQD)
  88.             gColorIcons[i]=GetCIcon(128+i);
  89.         gBWIcons[i]=GetIcon(128+i);
  90.     }
  91.     for (i=0; i<15; i++)
  92.     {
  93.         if (gHasColorQD)
  94.             gPlainColors[i]=GetPixPat(141+i);
  95.         gBWPatterns[i]=GetPattern(126+i);
  96.     }
  97.     for (i=0; i<12; i++)
  98.         for (j=0; j<5; j++)
  99.             for (k=0; k<5; k++)
  100.                 gShapes[i][j][k]=0;
  101.     gShapes[0][0][0]=gShapes[0][1][0]=gShapes[0][2][0]=gShapes[0][3][0]=gShapes[0][4][0]=1;
  102.     gShapes[1][0][0]=gShapes[1][1][0]=gShapes[1][2][0]=gShapes[1][3][0]=gShapes[1][3][1]=1;
  103.     gShapes[2][0][0]=gShapes[2][1][0]=gShapes[2][2][0]=gShapes[2][2][1]=gShapes[2][2][2]=1;
  104.     gShapes[3][0][1]=gShapes[3][1][0]=gShapes[3][1][1]=gShapes[3][1][2]=gShapes[3][2][1]=1;
  105.     gShapes[4][0][0]=gShapes[4][0][1]=gShapes[4][1][0]=gShapes[4][1][1]=gShapes[4][1][2]=1;
  106.     gShapes[5][0][0]=gShapes[5][1][0]=gShapes[5][1][1]=gShapes[5][2][1]=gShapes[5][2][2]=1;
  107.     gShapes[6][0][0]=gShapes[6][0][2]=gShapes[6][1][0]=gShapes[6][1][1]=gShapes[6][1][2]=1;
  108.     gShapes[7][0][1]=gShapes[7][0][2]=gShapes[7][1][1]=gShapes[7][2][0]=gShapes[7][2][1]=1;
  109.     gShapes[8][0][0]=gShapes[8][1][0]=gShapes[8][2][0]=gShapes[8][2][1]=gShapes[8][3][0]=1;
  110.     gShapes[9][0][1]=gShapes[9][1][0]=gShapes[9][1][1]=gShapes[9][2][1]=gShapes[9][2][2]=1;
  111.     gShapes[10][0][0]=gShapes[10][0][1]=gShapes[10][0][2]=gShapes[10][1][1]=gShapes[10][2][1]=1;
  112.     gShapes[11][0][0]=gShapes[11][1][0]=gShapes[11][2][0]=gShapes[11][2][1]=gShapes[11][3][1]=1;
  113.  
  114.     gameFile.name[0]=0x00;
  115.     deleteTheThing=FALSE;
  116.     freshClick=TRUE;
  117.     
  118.     CountAppFiles(&i, &j);
  119.     if ((j>0) && (i==0))
  120.     {
  121.         GetAppFiles(1, &myFile);
  122.         MyMakeFSSpec(myFile.vRefNum, 0, myFile.fName, &gameFile);
  123.         GenericOpen(&gameFile);
  124.     }
  125. }
  126.  
  127. void NewGame(void)
  128. {
  129.     CalculateRowsandCols();
  130.     InitGameVariables();
  131.     OpenNewGame();
  132. }
  133.  
  134. void WinGame(void)
  135. {
  136.     DoSound(sound_wingame);
  137.     PositionDialog('ALRT', congratsAlert);
  138.     Alert(congratsAlert, 0L);
  139. }
  140.  
  141. void GameUndo(void)
  142. {
  143.     int            i,j;
  144.     
  145.     if (gNumHilited)
  146.     {
  147.         gNumHilited = 0;
  148.         for (i=0; i<gNumRows; i++)
  149.             for (j=0; j<gNumCols; j++)
  150.                 if (Board[i][j]==-2)
  151.                     Board[i][j]=(gCurrentColor==-2) ? -1 : gCurrentColor;
  152.         gCurrentColor=-2;
  153.     }
  154.     else if (gNumPlayed)
  155.     {
  156.         DoSound(sound_undo);
  157.         gNumPlayed--;
  158.         PieceUsed[PiecesPlayed[gNumPlayed]] = FALSE;
  159.         for (i=0; i<gNumRows; i++)
  160.             for (j=0; j<gNumCols; j++)
  161.                 if (Board[i][j]==PiecesPlayed[gNumPlayed])
  162.                     Board[i][j]=-1;
  163.     }
  164.     UpdateBoard();
  165. }
  166.  
  167. void GameEvent(void)
  168. {
  169.     Point        mouseLoc;
  170.     Rect        boardRect;
  171.     int            i,j;
  172.     int            iter,utter;
  173.     int            whichWay;
  174.     Rect        smallRect;
  175.     Boolean        didSomething;
  176.     Boolean        freshClick;
  177.     Boolean        justRemovedOne;
  178.     
  179.     SetRect(&boardRect,5,gWindowHeight-(gNumRows*25)-5,gWindowWidth-5,gWindowHeight-5);
  180.     GetMouse(&mouseLoc);
  181.     
  182.     if (PtInRect(mouseLoc,&boardRect))
  183.     {
  184.         whichWay=0;
  185.         justRemovedOne=FALSE;
  186.         freshClick=TRUE;
  187.         
  188.         while (StillDown())
  189.         {
  190.             GetMouse(&mouseLoc);
  191.             if (PtInRect(mouseLoc,&boardRect))
  192.             {
  193.                 j=(mouseLoc.h-5)/25;
  194.                 i=(mouseLoc.v-gWindowHeight+(gNumRows*25)+5)/25;
  195.                 smallRect.top=gWindowHeight-((gNumRows-i)*25)-4;
  196.                 smallRect.bottom=smallRect.top+24;
  197.                 smallRect.left=6+j*25;
  198.                 smallRect.right=smallRect.left+24;
  199.  
  200.                 switch (Board[i][j])
  201.                 {
  202.                     case -2:
  203.                     {
  204.                         if (gCurrentColor!=-2)
  205.                         {
  206.                             if (freshClick)
  207.                             {
  208.                                 if (Ticks-oldTicks<DoubleTime)
  209.                                 {
  210.                                     RemovePiece();
  211.                                     justRemovedOne=TRUE;
  212.                                 }
  213.                                 else
  214.                                     GameUndo();
  215.                                 oldTicks=0;
  216.                                 freshClick=FALSE;
  217.                             }
  218.                         }
  219.                         else if (whichWay>=0)
  220.                         {
  221.                             gNumHilited--;
  222.                             Board[i][j]=-1;
  223.                             if (GetWindowDepth() > 2)
  224.                                 FillCRect(&smallRect,gPlainColors[1]);
  225.                             else
  226.                                 FillRect(&smallRect,*gBWPatterns[1]);
  227.                             whichWay=1;
  228.                         }
  229.                         break;
  230.                     }
  231.                     case -1:
  232.                     {
  233.                         if ((whichWay<=0) && (gCurrentColor==-2) && (!justRemovedOne))
  234.                         {
  235.                             gNumHilited++;
  236.                             Board[i][j]=-2;
  237.                             if (GetWindowDepth() > 2)
  238.                                 FillCRect(&smallRect,gPlainColors[0]);
  239.                             else
  240.                                 FillRect(&smallRect,*gBWPatterns[0]);
  241.                             whichWay=-1;
  242.                             freshClick=FALSE;
  243.                         }
  244.                         break;
  245.                     }
  246.                     default:
  247.                     {
  248.                         if ((whichWay==0) && (gNumHilited==0) && (Board[i][j]!=12))
  249.                         {
  250.                             gCurrentColor=Board[i][j];
  251.                             for (iter=0; iter<gNumRows; iter++)
  252.                                 for (utter=0; utter<gNumCols; utter++)
  253.                                     if (Board[iter][utter]==gCurrentColor)
  254.                                         Board[iter][utter]=-2;
  255.                             gNumHilited=5;
  256.                             oldTicks=Ticks;
  257.                             UpdateBoard();
  258.                             freshClick=FALSE;
  259.                         }
  260.                         break;
  261.                     }
  262.                 }
  263.             }
  264.         }
  265.     }
  266.     else
  267.     {
  268.         i=0;
  269.         didSomething=FALSE;
  270.         do
  271.         {
  272.             boardRect.top=10;
  273.             boardRect.bottom=42;
  274.             boardRect.left=(gWindowWidth/2)-((3-i)*34)+1;
  275.             boardRect.right=boardRect.left+32;
  276.             if ((!PieceUsed[i]) && (PtInRect(mouseLoc,&boardRect)))
  277.             {
  278.                 if (gNumHilited==5)
  279.                     j=PlacePiece(i);
  280.                 didSomething=TRUE;
  281.             }
  282.             else
  283.             {
  284.                 boardRect.top+=40;
  285.                 boardRect.bottom+=40;
  286.                 if ((!PieceUsed[i+6]) && (PtInRect(mouseLoc, &boardRect)))
  287.                 {
  288.                     if (gNumHilited==5)
  289.                         j=PlacePiece(i+6);
  290.                     didSomething=TRUE;
  291.                 }
  292.             }
  293.             i++;
  294.         }
  295.         while ((!didSomething) && (i<6));
  296.         
  297.         if (didSomething)
  298.         {
  299.             if (gNumHilited==0)
  300.             {
  301.                 DoSound(sound_error);
  302.                 ParamText("\pYou must first highlight on the board where this piece \
  303. should go.","","","");
  304.                 PositionDialog('ALRT', generalAlert);
  305.                 StopAlert(generalAlert,0L);
  306.             }
  307.             else if (gNumHilited!=5)
  308.             {
  309.                 DoSound(sound_error);
  310.                 ParamText("\pThat piece does not match what you have highlighted \
  311. on the board.","","","");
  312.                 PositionDialog('ALRT', generalAlert);
  313.                 StopAlert(generalAlert,0L);
  314.             }
  315.             else if (j>=0)
  316.             {
  317.                 DoSound(sound_placepiece);
  318.                 PieceUsed[j]=TRUE;
  319.                 PiecesPlayed[gNumPlayed]=j;
  320.                 gNumPlayed++;
  321.                 gNumHilited=0;
  322.                 UpdateBoard();
  323.                 if (gNumPlayed==12)
  324.                     WinGame();
  325.             }
  326.             else if (j==-1)
  327.             {
  328.                 DoSound(sound_error);
  329.                 ParamText("\pThat piece does not match what you have highlighted \
  330. on the board.","","","");
  331.                 PositionDialog('ALRT', generalAlert);
  332.                 StopAlert(generalAlert,0L);
  333.             }
  334.         }
  335.     }
  336. }
  337.  
  338. void GameKeyEvent(char charPressed)
  339. {
  340.     int            pieceNum;
  341.     
  342.     if ((charPressed==0x0d) || (charPressed==0x03))
  343.     {
  344.         if (gNumHilited==5)
  345.         {
  346.             if (gCurrentColor==-2)
  347.                 PlaceUnknownPiece();
  348.         }
  349.         else if (gNumHilited>0)
  350.         {
  351.             DoSound(sound_error);
  352.             ParamText("\pThat is not one of the 12 Pentominoes pieces.","","","");
  353.             PositionDialog('ALRT', generalAlert);
  354.             StopAlert(generalAlert,0L);
  355.         }
  356.     }
  357.     if (charPressed==0x08)
  358.     {
  359.          if ((gNumHilited==5) && (gCurrentColor!=-2))
  360.             RemovePiece();
  361.         else if (gNumHilited>0)
  362.             GameUndo();
  363.     }
  364. }
  365.  
  366. void ShutDownGame(void)
  367. {
  368.     int            i;
  369.     
  370.     for (i=0; i<12; i++)
  371.     {
  372.         if (gHasColorQD)
  373.             DisposeCIcon(gColorIcons[i]);
  374.         ReleaseResource(gBWIcons[i]);
  375.     }
  376.     for (i=0; i<15; i++)
  377.     {
  378.         if (gHasColorQD)
  379.             DisposePixPat(gPlainColors[i]);
  380.         ReleaseResource(gBWPatterns[i]);
  381.     }
  382. }
  383.